package com.wohuijituan.whcsmerchant.adapter;

import android.content.Context;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.bean.GoodsInOrderBean;
import com.wohuijituan.whcsmerchant.bean.OrderListBean;
import com.wohuijituan.whcsmerchant.myview.MyListViewInListView;

import java.util.List;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/19 10:59
 * 版本：V1.0
 * 修改历史：
 */
public class OrderListAdapter extends MyBaseAdapter<OrderListBean.DataEntity.OrderEntity> implements View.OnClickListener{
    public OrderListAdapter(Context ctx, List<OrderListBean.DataEntity.OrderEntity> dataList) {
        super(ctx, dataList);
    }
    private final int ORDER_STATE_UNRECEIVE = 20;//已付款
    private final int ORDER_STATE_PREPARE = 30;//备货中
    private final int ORDER_STATE_DELIVER = 31; //配送中
    private final int ORDER_STATE_DELIVER_OVER = 32;//已送达
    private final int ORDER_STATE_DONE = 40;//用户已收货
    private final int ORDER_STATE_REFUND = 70;//申请退款
    private final int ORDER_STATE_REFUND_OVER = 80;//退款完成

    public void setItemBtnClickListener(MyOnItemBtnClickListener itemBtnClickListener) {
        this.itemBtnClickListener = itemBtnClickListener;
    }
    private MyOnItemBtnClickListener itemBtnClickListener;//一定要记得设置,否则会发生错误

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView==null){
            convertView = View.inflate(ctx, R.layout.item_order_list,null);
            vh = new ViewHolder(convertView);
            vh.btnOrderReceive.setOnClickListener(this);
            vh.btnOrderReceive.setTag(position);//给确认收货按钮添加Tag
            convertView.setTag(vh);

        }else {
            vh = (ViewHolder) convertView.getTag();
            vh.btnOrderReceive.setTag(position);
        }
        OrderListBean.DataEntity.OrderEntity orderData = dataList.get(position);
        vh.tvOrderTime.setText(orderData.getO_ArrivaTime());
        vh.tvOrderName.setText(orderData.getO_RealName()
                + "（"
                + orderData.getO_Phone()
                + "）");
        vh.tvOrderAddress.setText(orderData.getO_Address());
        vh.tvOrderPrice.setText("￥"+orderData.getO_TotalPrice()+"");

        //给订单中的商品列表设置适配器
        List<GoodsInOrderBean> goodsList = orderData.getDetail();
        if (vh.itemGoodsAdapter == null) {
            vh.itemGoodsAdapter = new ItemGoodsAdapter(ctx,goodsList);
            vh.lvOrderGoodsItem.setAdapter(vh.itemGoodsAdapter);
        } else {
            vh.itemGoodsAdapter.replace(goodsList);
        }
        //设置订单状态
        setOrderState(orderData.getO_Status(),vh);
        int number = position + 1;
        vh.tvOrderNumber.setText(number+"");
        return convertView;
    }
    /**
     * 设置订单状态
     * @param orderState 0为未接单订单,1为准备发货订单,2为已完成订单
     * @param vh
     */
    private void setOrderState(int orderState,ViewHolder vh){
        switch (orderState){
            case ORDER_STATE_UNRECEIVE:
                //  已付款订单
                vh.btnOrderReceive.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setVisibility(View.GONE);
                vh.btnOrderReceive.setText(R.string.order_state_receive);
                break;
            case ORDER_STATE_PREPARE:
                //  2016/12/29 备货中订单
                vh.btnOrderReceive.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setVisibility(View.GONE);
                vh.btnOrderReceive.setText(R.string.order_state_prepare);
                break;
            case ORDER_STATE_DELIVER:
                // TODO: 2016/12/29 送货中订单
                vh.btnOrderReceive.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setVisibility(View.GONE);
                vh.btnOrderReceive.setText(R.string.order_state_deliver);
                break;
            case ORDER_STATE_DELIVER_OVER:
                // TODO: 2016/12/29 已送达订单
                vh.btnOrderReceive.setVisibility(View.GONE);
                vh.tvOrderPrepare.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setText(R.string.order_state_confirm);
                break;
            case ORDER_STATE_DONE:
                //  2016/12/29 用户已确认收货订单
                vh.btnOrderReceive.setVisibility(View.GONE);
                vh.tvOrderPrepare.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setText(R.string.order_state_confirm_over);
                break;
            case ORDER_STATE_REFUND:
                // 2016/12/29 用户申请退款订单
                vh.btnOrderReceive.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setVisibility(View.GONE);
                vh.btnOrderReceive.setText(R.string.order_state_refund);
                break;
            case ORDER_STATE_REFUND_OVER:
                // TODO: 2016/12/29 退款完成
                vh.btnOrderReceive.setVisibility(View.GONE);
                vh.tvOrderPrepare.setVisibility(View.VISIBLE);
                vh.tvOrderPrepare.setText(R.string.order_state_refund_over);
                break;
            default:
                showToast(ctx,"订单状态错误:"+orderState);
        }

    }

    @Override
    public void onClick(View v) {

        int position = (int) v.getTag();
        if(itemBtnClickListener != null){
            itemBtnClickListener.onClick(position);

        }else {
            showToast(ctx,"没有设置itemBtnClickListener监听");
        }
    }

    /**
     * 监听接口,用来传输被点击控件所在item的position
     */
    public interface MyOnItemBtnClickListener{
        public void onClick(int position);
    }
    public class ViewHolder {
        public final TextView tvOrderNumber;
        public final TextView tvOrderTime;
        public final TextView tvOrderName;
        public final TextView tvOrderAddress;
        public final TextView tvOrderPrice;
        public final Button btnOrderReceive;
        public final TextView tvOrderPrepare;
        public ItemGoodsAdapter itemGoodsAdapter;
        public final MyListViewInListView lvOrderGoodsItem;
        public final View root;

        public ViewHolder(View root) {
            tvOrderNumber = (TextView) root.findViewById(R.id.tv_order_number);
            tvOrderTime = (TextView) root.findViewById(R.id.tv_order_time);
            tvOrderName = (TextView) root.findViewById(R.id.tv_order_name);
            tvOrderAddress = (TextView) root.findViewById(R.id.tv_order_address);
            tvOrderPrice = (TextView) root.findViewById(R.id.tv_order_price);
            btnOrderReceive = (Button) root.findViewById(R.id.btn_order_receive);
            tvOrderPrepare = (TextView) root.findViewById(R.id.tv_order_prepare);
            lvOrderGoodsItem = (MyListViewInListView) root.findViewById(R.id.lv_order_goods_list);
            this.root = root;
        }
    }
}
